<template>
  <div class="TabApproval">
    <el-table-form ref="form" :model="query">
      <el-form-item class="merge-item" label="条件类型">
        <el-select v-model="query.csmType" class="mr10">
          <el-option label="客户电话" value="csmPhone" />
          <el-option label="客户编号" value="csmCode" />
          <el-option label="客户名称" value="csmName" />
          <el-option label="客户地址" value="csmAddress" />
          <el-option label="对账单号" value="billCode" />
        </el-select>
        <el-input v-model="query.csmText" placeholder="请输入" />
      </el-form-item>

      <template v-slot:btns>
        <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
      </template>
    </el-table-form>
    <div class="mb20">
      <el-button
        type="primary"
        icon="el-icon-document-delete"
        @click="exportFile"
      >导出</el-button>
    </div>

    <!-- 表格内容 -->
    <lb-table
      ref="lbTable"
      v-el-height-adaptive-table="{ bottomOffset: 10 }"
      border
      highlight-current-row
      element-loading-text="加载中"
      header-row-class-name="table-row-name"
      :column="column"
      :data="tableData.data"
      pagination
      :total="tableData.total"
      custom-column
      @fetch="fetchData"
    />
    <OrderDetailDialog ref="OrderDetailDialogRef" @refresh="search" />
  </div>
</template>

<script>
  import OrderDetailDialog from './components/OrderDetailDialog.vue'
  import {
    queryExportDomain,
    queryMonthAccCsmMonthlyTrackTabApprovalList,
    monthAccCsmMonthlyTrackTabApprovalExport,
    monthAccCsmMonthlyTrackTabApprovalAudit
  } from '@/api'
  // import dayjs from "dayjs";
  import { mergeAsyncFunc } from '@/utils/mergeAsyncFunc'

  export default {
    name: 'TabApproval',
    components: {
      OrderDetailDialog
    },
    data() {
      return {
        exportDomain: '',
        rowData: {}, // 表格当前行数据
        query: {
          csmType: '',
          csmText: ''
        },
        tableData: {
          total: 0,
          data: []
        }
      }
    },
    computed: {
      column: function() {
        return [
          {
            prop: 'billCode',
            label: '对账单号',
            width: 200,
            render: (h, { row }) => (
            <el-link underline={false} onClick={() => this.openDetail(row)}>
              {row.billCode}
            </el-link>
          )
          },
          // {
          //   prop: 'csmId',
          //   label: '客户id',
          //   width: 150
          // },
          {
            prop: 'csmCode',
            label: '客户编号',
            width: 110
          },
          {
            prop: 'csmName',
            label: '客户名称',
            width: 200
          },
          {
            prop: 'csmPhone',
            label: '电话',
            width: 110
          },
          {
            prop: 'csmAddress',
            label: '地址',
            width: 200
          },

          {
            prop: 'orgName',
            label: '挂帐机构',
            width: 150
          },
          {
            prop: 'applicantName',
            label: '上报人',
            width: 120
          },
          {
            prop: 'amount',
            align: 'right',
            width: 150,
            label: '上报减免金额'
          },
          {
            label: '操作',
            width: 80,
            align: 'center',
            fixed: 'right',
            render: (h, { row, $index }) => {
              return (
              <div>
                <el-link
                  size='mini'
                  underline={false}
                  class='mr0'
                  onClick={() => this.audit(row)}
                >
                  审批
                </el-link>
              </div>
              )
            }
          }
        ]
      }
    },
    created() {
      this.getExportDomain()
    },
    methods: {
      // 获取导出域名
      getExportDomain() {
        mergeAsyncFunc(queryExportDomain).then((res) => {
          this.exportDomain = res.data
        })
      },

      caStatusText(val) {
        switch (val) {
        case 10:
          return '待结款'
        case 20:
          return '待核销'
        case 30:
          return '待销单'
        case 40:
          return '驳回'
        }
      },

      _getQuery(obj) {
        const query = {}
        for (const key in obj) {
          if (obj.hasOwnProperty(key)) {
            if (obj[key] !== '') {
              query[key] = obj[key]
            }
          }
        }
        return query
      },
      async fetchData({ currentPage, pageSize }) {
        const query = {
          ...this._getQuery(this.query),
          currentPage,
          pageSize
        }
        if (this.query.csmText) query[this.query.csmType] = this.query.csmText
        delete query.csmType
        delete query.csmText
        const res = await queryMonthAccCsmMonthlyTrackTabApprovalList(query)
        this.tableData.data = res.data.rows
        if (currentPage === 1) this.tableData.total = res.data.total
      },

      // 导出
      async exportFile() {
        const _this = this
        this.$confirm(`确定导出数据？`, '提示', {
          type: 'info'
        }).then(() => {
          const copyColumn = JSON.parse(JSON.stringify(this.column))
          copyColumn.pop()
          const str = {}
          copyColumn.forEach((item) => {
            str[item.prop] = item.label
          })
          monthAccCsmMonthlyTrackTabApprovalExport({
            paramInfo: _this._getQuery(_this.query),
            kv: str // 表格列
          }).then((res) => {
            window.open(`${this.exportDomain}/page/export/frame?key=${res.data}`, '_blank')
          })
        })
      },
      search() {
        this.$refs.lbTable.fetch({ currentPage: 1 })
      },
      // 打开单据详情
      openDetail(row) {
        this.$refs['OrderDetailDialogRef'].show(3, row)
      },
      audit(row) {
        this.$confirm(
          `<p style='font-size: 16px;'>本次申请减免金额:</p><div style='font-size: 30px;color: red;text-align: center;margin-top:20px;'>¥${row.amount}</div>`,
          '减免差额审批',
          {
            confirmButtonText: '通过',
            cancelButtonText: '驳回',
            distinguishCancelAndClose: true,
            dangerouslyUseHTMLString: true
          }
        )
          .then(() => {
            this.auditSubmit(row, 2)
          })
          .catch((action) => {
            if (action === 'cancel') {
              this.auditSubmit(row, 3)
            }
          })
      },
      async auditSubmit(row, billStatus) {
        const params = {
          billCode: row.billCode,
          billStatus: billStatus
        }
        const { status } = await monthAccCsmMonthlyTrackTabApprovalAudit(params)
        if (status === 200) {
          this.$notify({
            title: '提示',
            message: `操作成功`,
            type: 'success',
            duration: 2000
          })
          this.search()
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
.TabApproval {
  .el-form-item .el-form-item {
    margin-bottom: 0;
  }
  .merge-item {
    /deep/ .el-form-item__content {
      display: flex;
    }
  }
  .btn-group {
    display: flex;
    justify-content: space-between;
  }
}
</style>
